// pages/course/lp/lp.js
var util = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    indexHidden: false,
    searchResultHidden: true,
    sorts: ["综合排序", "销量排序", "价格从高到低", "价格从低到高"],
    params: {
      Param: { UserID: "", SortType: 0, PriceType: 0, Subject: 0, ShowIndexNum: 20, PageIndex: 1 },
      Action: 12001
    },
    params2: {
      Param: {
        SearchKey: "",
        InPackeage: 0,
        InLive: 1,
        InEduVideo: 0,
        InEduVoice: 0,
        InCurr: 0,
        PageIndex: 1,
        ShowIndexNum: 50
      },
      Action: 18900
    },
    LiveList: [],
    swiperHeight: "204",




    // top-search-end

    // swiper-begin
    swiper: {
      urls: [],
      dots: true,
      autoplay: true,
      interval: 2000,
      duration: 500
    }
    // swiper-end

  },
  //component-select
  onShowResult: function () {
    var that = this;
    var params2 = this.data.params2;
    params2.Param.SearchKey = this.selectComponent('#select').onGetSearchKey();
    util.reqMedia(params2, function (value) {
      that.setData({
        LiveList: value.LiveList
      });
    });
    this.setData({
      searchResultHidden: false,
      indexHidden: true
    });

  },
  onPickerChange: function () {
    var index = this.selectComponent('#select').onGetIndex();
    var params = this.data.params;
    params.Param.SortType = index;
    var page = this;
    util.reqMedia(params, function (value) {
      page.setData({
        LiveList: value.LiveList
      });
    });
    this.setData({
      searchResultHidden: true,
      indexHidden: false
    });
  },
  onSelect: function () {
    this.setData({
      indexHidden: true
    });
  },
  onConfirm: function () {
    var ids = this.selectComponent('#select').onGetIds();
    var params = this.data.params;
    params.Param.AdaptationType = ids.AdaptationType;
    params.Param.PriceType = ids.PriceType;
    params.Param.Subject = ids.Subject;
    var page = this;
    util.reqMedia(params, function (value) {
      page.setData({
        LiveList: value.LiveList
      });
    });
    this.setData({
      searchResultHidden: true,
      indexHidden: false
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '稍等',
    });
    //get windowWidth
    var swiperHeight = getApp().globalData.windowWidth * 202 * 2 / 720;
    this.setData({
      swiperHeight: swiperHeight
    });


    var that = this;
    var params = this.data.params;
    params.Param.UserID = getApp().globalData.userId;
    util.reqMedia(params, function (value) {
      that.setData({
        LiveList: value.LiveList
      });
      wx.hideLoading();
    });



    //get swipers
    const params3 = {
      Param: {},
      Action: 3710
    };
    util.reqMedia(params3, function (value) {
      var urls = [];
      var LiveAdvertList = value.LiveAdvertList;

      for (let i = 0; i < LiveAdvertList.length; i++) {
        urls[i] = LiveAdvertList[i]['ImageUrl'];
      }
      var swiper = that.data.swiper;
      swiper.urls = urls;
      that.setData({
        swiper: swiper
      });



    });


  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 
   * 
   */
  onPullDownRefresh: function () {

    var that = this;
    var params = this.data.params;
    params.Param.UserID = getApp().globalData.userId;
    util.reqMedia(params, function (value) {
      that.setData({
        LiveList: value.LiveList
      });
    });



    //get swipers
    const params3 = {
      Param: {},
      Action: 3710
    };
    util.reqMedia(params3, function (value) {
      var urls = [];
      var LiveAdvertList = value.LiveAdvertList;

      for (let i = 0; i < LiveAdvertList.length; i++) {
        urls[i] = LiveAdvertList[i]['ImageUrl'];
      }
      var swiper = that.data.swiper;
      swiper.urls = urls;
      that.setData({
        swiper: swiper
      });

    });
    wx.stopPullDownRefresh();
  }
  
})